package com.example.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.photomessage.entity.PhotoMessage;
import com.example.user.bean.UserPhotoMsg;
import com.example.user.entity.User;
import com.example.user.mapper.UserMapper;
import com.example.user.service.IUserService;
import com.example.user.userUtil.UserMessage;
import com.example.util.PageResult;
import com.example.util.PasswordUtil;
import com.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 贾明祯
 * @ClassName UserServiceImpl
 * @Date 2020/8/17 11:12
 * @Introduce
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> listPage(int pageNum, int pageSize, User user) {
        Page<User> page = new Page<User>(pageNum, pageSize);
        QueryWrapper<User> ew = new QueryWrapper<User>();
        IPage iPage = userMapper.selectPage(page,ew);
        return new PageResult<User>(iPage.getRecords(), iPage.getTotal());
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/17 14:16
     * @Introduce: 用户注册，判断账号|手机号|邮箱 不能被重复注册
     * 每项都需要单独提示用户，否者无法分清某一项是重复已存在的
     */
    @Override
    public Result registerUser(User user) {
        /**
         * 判断账号是否存在
         */
        QueryWrapper<User> account = new QueryWrapper<>();
        account.eq("account",user.getAccount());
        Integer integer = userMapper.selectCount(account);
        if(integer>0){
            return Result.error(404, "该账号已被注册");
        }

        /**
         * 判断手机号是否存在
         */
        QueryWrapper<User> phone = new QueryWrapper<>();
        phone.eq("phone",user.getPhone());
        integer = userMapper.selectCount(phone);
        if(integer>0){
            return Result.error(404, "该号码已被注册");
        }

        /**
         * 判断邮箱是否存在
         */
        QueryWrapper<User> email = new QueryWrapper<>();
        email.eq("email",user.getEmail());
        integer = userMapper.selectCount(email);
        if(integer>0){
            return Result.error(404, "该邮箱已被注册");
        }
        /**
         * 获取6位的盐
         */
        String salt = PasswordUtil.getSalt();
        user.setSalt(salt);
        /**
         * 对用户密码进行加盐加密设置
         */
        user.setPassword(PasswordUtil.getMD5Str(user.getPassword(),salt));

        if(userMapper.insert(user)>0){
            return  Result.success(200,"注册成功");
        }
        return  Result.success(500,"注册失败");
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/17 15:31
     * @Introduce: 用户账号登录
     */
    @Override
    public User userLogin(String account,String password) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        /**
         * 根据 账号 登录用户
         */
        qw.eq("account",account);
        User user = userMapper.selectOne(qw);
        if(user!=null){
            /**
             * 判断输入的密码是否和该用户密码相等
             */
            if(user.getPassword().equals(PasswordUtil.getMD5Str(password,user.getSalt()))){
                return user;
            }
        }
        return null;
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/21 9:11
     * @Introduce: 手机登录
     */
    @Override
    public User phoneLogin(String phone) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("phone",phone);
        return userMapper.selectOne(qw);
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/17 14:39
     * @Introduce: 修改用户信息
     */
    @Override
    public Result changeUser(User user, HttpSession session) {
        user.setId(UserMessage.userId(session));
        /**
         * 根据id修改用户信息
         */
        if(userMapper.updateById(user)>0){
            return Result.success(200,"修改成功");
        }
        return Result.error(500,"修改失败");
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/17 15:03
     * @Introduce: 修改密码
     */
    @Override
    public Result changeUserPassword(String oldPassWord, String newPassword, HttpSession session) {
        User newUser = new User();
        /**
         * 根据session存储的用户id查询对象
         */
        User user = userMapper.selectById(UserMessage.userId(session));
        if(user!=null){
            newUser.setId(user.getId());
            /**
             * 获取盐
             */
            String salt = user.getSalt();
            /**
             * 判断输入的旧密码是否与用户密码相同
             */
            if(user.getPassword().equals(PasswordUtil.getMD5Str(oldPassWord,salt))){
                /**
                 * 新密码的加盐加密设置
                 */
                newUser.setPassword(PasswordUtil.getMD5Str(newPassword,salt));
                userMapper.updateById(newUser);
                return Result.success(200,"修改成功");
            }
        }

        return Result.error(500,"修改失败");
    }

    /**
      * @Author: 贾明祯
      * @Date: 2020/8/21 16:54
      * @Introduce: 查询所有用户
      */
    @Override
    public List<User> selectList() {
        QueryWrapper<User> qw = new QueryWrapper<>();
        return userMapper.selectList(qw);
    }

    @Override
    public List<UserPhotoMsg> showUserList(Integer id) {
        return userMapper.showUserList(id);
    }

    @Override
    public List<PhotoMessage> showPhotos(Integer id) {
        return userMapper.showPhotos(id);
    }

}
